﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AliSMS
{
    public partial class FormTempLateEdit : Form
    {
        public int eid;
        string sql = "";
        InitFile init = new InitFile();
        MsSqlHelper mssql = new MsSqlHelper();
        SQLiteHelper sqlite = new SQLiteHelper();
        DataTable dt = new DataTable();

        public FormTempLateEdit()
        {
            InitializeComponent();
        }

        private void FormTempLateEdit_Load(object sender, EventArgs e)
        {
            if (eid != 0)
            {
                sql = "select * from [TempLate] where id="+eid+"";
                dt = sqlite.ExecuteDt(sql);
                if (dt.Rows.Count > 0)
                {
                    txttitle.Text = dt.Rows[0]["TempLateTitle"].ToString();
                    txtcode.Text = dt.Rows[0]["TempLateCode"].ToString();
                    txtquery.Text = dt.Rows[0]["QuerySQL"].ToString();
                    txtupdate.Text = dt.Rows[0]["UpdateSQL"].ToString();
                    txtsms.Text = dt.Rows[0]["TempLateSMS"].ToString();
                    cBbeactive.Checked = dt.Rows[0]["beactive"].ToString()=="1"?true:false;
                }
            }
        }
        

        private void btnSave_Click(object sender, EventArgs e)
        {
            string title = txttitle.Text;
            string code = txtcode.Text;
            string query = txtquery.Text.Replace("'","''");
            string update = txtupdate.Text.Replace("'", "''");
            string sms = txtsms.Text.Replace("'", "''");
            int beactive =cBbeactive.Checked?1:0;
            string errinfo="";
            bool flag = mssql.ValidateSQL(txtquery.Text, ref errinfo);
            if (!flag)
            {
                MessageBox.Show("查询SQL检查不通过，请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (eid == 0) {
                sql = string.Format("insert into [TempLate](TempLateTitle,TempLateCode,TempLateSMS,QuerySQL,UpdateSQL,Beactive)values('{0}','{1}','{2}','{3}','{4}',{5})",title,code ,sms , query,update ,beactive);
            }
            else
            {
                sql =string.Format("update [TempLate] set TempLateTitle='{0}',TempLateCode='{1}',TempLateSMS='{2}',QuerySQL='{3}',UpdateSQL='{4}',Beactive={5} where id={6}",title,code ,sms,query,update ,beactive,eid);
            }
            try
            {
                //init.WriteLog(sql, false);
                if (sqlite.ExecuteSql(sql) > 0)
                {
                    MessageBox.Show("添加/修改成功！");
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                init.WriteLog(ex.ToString(), true);
                MessageBox.Show(ex.Message.ToString());
            }  
        }
    }
}
